package com.boot.app.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boot.api.ApiResult;
import com.boot.app.base.domain.BaseUserRoleRelation;
import com.boot.app.base.service.IBaseUserRoleRelationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "UserRoleRelationController", description = "用户角色对应关系")
@RestController
@RequestMapping("base/user/role/relation")
public class BaseUserRoleRelationController {

    private final IBaseUserRoleRelationService service;

    @Autowired
    public BaseUserRoleRelationController(
        IBaseUserRoleRelationService service
    ){
        this.service = service;
    }

    @Operation(summary = "获取用户的角色权限")
    @GetMapping("list")
    public ApiResult<List<BaseUserRoleRelation>> getUserRoleRelationList(BaseUserRoleRelation relation){
        LambdaQueryWrapper<BaseUserRoleRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseUserRoleRelation::getUserId, relation.getUserId());
        List<BaseUserRoleRelation> list = service.list(wrapper);
        return ApiResult.success(list);
    }

    @Operation(summary = "为用户分配角色权限")
    @PostMapping("list")
    public ApiResult<?> postRoleAppRelation(@RequestParam("userId") long userId, @RequestParam(value = "roleIds", required = false) long[] roleIds){
        service.saveBatch(userId, roleIds);
        return ApiResult.success();
    }

}
